package cn.wonderland.base.util;

import cn.wonderland.base.cons.JwtRasHolder;
import cn.wonderland.base.jwt.JwtUtils;
import cn.wonderland.base.jwt.Payload;
import cn.wonderland.base.jwt.RsaUtils;
import cn.wonderland.base.jwt.UserInfo;
import cn.wonderland.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {

    public static Logininfo getLogininfo(HttpServletRequest request){
        String token = request.getHeader("token");
        PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource(JwtRasHolder.INSTANCE.getJwtPub()).getFile());
        Payload<UserInfo> payload = JwtUtils.getInfoFromToken(token, publicKey, UserInfo.class);
        if (null == payload || null == payload.getUserInfo()){
            return null;
        }
        return payload.getUserInfo().getLogininfo();
    }



    // 获取userinfo
    public static UserInfo getUserInfo() {
        return ThreadLocalUtil.getUserInfo();
    }

    // 获取logininfo
    public static Logininfo getLogininfo() {
        return ThreadLocalUtil.getUserInfo().getLogininfo();
    }
}